package com.sheng.viewResolver;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

import java.util.Locale;

public class PdfViewResolver implements ViewResolver {
    private String viewPackage;

    public void setViewPackage(String viewPackage) {
        this.viewPackage = viewPackage;
    }
    @Override
    public View resolveViewName(String viewName, Locale locale) throws Exception {
        try {
            int lastDotPosition = viewName.lastIndexOf(".");
            if(lastDotPosition > 0){
                viewName = viewName.substring(0,lastDotPosition);
            }
            Class<?> viewClass = Class.forName(this.viewPackage + "." + StringUtils.capitalize(viewName) + "Pdf");
            return (View) viewClass.getConstructor().newInstance();
        }catch (Exception e){
            return null;
        }
    }
}
